package com.jcj.jcjsharding.mybatis;


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import java.sql.Connection;

/**
 * sql拦截器
 */
@Slf4j
@Component
@Intercepts(@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}))
public class SqlStatementInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler handler = (StatementHandler) invocation.getTarget();
        BoundSql boundSql = handler.getBoundSql();
        log.info("【sql拦截器】sql statement:{}", boundSql.getSql());
        log.info("【sql拦截器】sql parameters:" + boundSql.getParameterObject());

        // TODO: 2024/12/19 修改sql
        //执行到mapper时，拦截了方法的操作，拦截的位置相对靠后。
        //UserMapper是Mapper，是。

        return invocation.proceed();
    }
}
